package com.example.rabbit.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 监听器 第一种写法
 * 使用注解 @RabbitListener(queues = "dead_letter_queue")
 * 必须手动创建交换机，队列，绑定关系
 *
 * 测试方法：测试类发送消息
 *
 * @author dell
 * @date 2022/08/10
 */
@Slf4j
@Component
public class DeadLetterConsumer {

    /**
     * 1. 手动创建，需在RabbitMQ中手动创建 dead_letter_queue 队列，绑定关系，否则报错
     *
     * @param message 消息
     */
    @RabbitListener(queues = "dead_letter_queue")
    public void receiveDeadLetterMessage(Message message, Channel channel) {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("方式一.监听死信队列。当前时间, {},收到消息:{}", new Date().toString(), msg);
    }

    @RabbitListener(queues = "normal_queue_a")
    public void receiveQueueA(Message message, Channel channel) {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("监听queue_a队列.当前时间, {},收到消息:{}", new Date().toString(), msg);
    }
}
